package com.lgr.store.controller;


import com.lgr.store.service.MaterialService;
import com.lgr.store.utils.AppointStateEnum;
import com.lgr.store.utils.ContextUtils;
import com.lgr.store.utils.LogJournal;
import com.lgr.store.utils.PageObject;
import com.lgr.store.vo.EmpVo;
import com.lgr.store.vo.MaterialTypeVo;
import com.lgr.store.vo.MaterialVo;
import com.lgr.store.vo.ProdTypeVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/material")
public class MaterialController {
    @Resource
    MaterialService materialService;
    @Resource
    HttpSession session;

    @RequestMapping(value="/json")
    @ResponseBody
    public Map json(MaterialVo material,HttpServletResponse response)throws IOException {
        MaterialVo materialList= materialService.getById(material.getMaterialId());
        Map map = new HashMap();
        map.put("materialList",materialList);
        return map;
    }
    //新增
    @RequestMapping("/init")
    @ResponseBody
    public Map add(HttpServletRequest request,MaterialVo material){
        //系统日志
        if(material.getMaterialId()==null){
            session.setAttribute(AppointStateEnum.LOG.getStateInfo(),LogJournal.setLogParam(1,material));
        }else{
            session.setAttribute(AppointStateEnum.LOG.getStateInfo(),LogJournal.setLogParam(2,material));
        }
        materialService.saveOrUpdate(material);
        Map<String,Object> map = new HashMap<String, Object>();
        return map;
    }
    //删除
    @RequestMapping("/materialdel")
    //表示使用ajax技术实现的方法
    @ResponseBody
    public Map del(HttpServletRequest request,MaterialVo material){
        String materialId = request.getParameter("materialId");
        //系统日志
        EmpVo emp = (EmpVo) session.getAttribute(AppointStateEnum.EMPS.getStateInfo());
        session.setAttribute(AppointStateEnum.LOG.getStateInfo(),LogJournal.setLogParam(3,material));

        materialService.materialDel(Integer.parseInt(materialId));
        Map<String,Object> maps = new HashMap<String, Object>();
        return maps;
    }
    //查询
    @RequestMapping("/materiallist")
    public String list(MaterialVo material, PageObject pager, ModelMap map){
        int cut = 0;
        cut = materialService.materialCount();
        pager.setTotalRows(cut);
        //material参数未使用，可以删除
        List<MaterialVo> materialList = materialService.list(material,pager);
        List<MaterialTypeVo> mtList = materialService.materialTypeList();
        List<ProdTypeVo> pList = materialService.prodList();

        map.addAttribute("material",material);
        map.addAttribute("materialList",materialList);
        map.addAttribute("materialTypeList",mtList);
        map.addAttribute("pList",pList);
        map.addAttribute("pager",pager);
        return "materialList";
    }
}
